﻿namespace FFF.Test.Application.AppServices.PublicService.Enum
{
    using System;
    using System.Collections.Generic;
    using System.IdentityModel.Tokens.Jwt;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Security.Claims;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Application.Services.Dto;
    using Abp.Auditing;
    using Abp.EntityFrameworkCore;
    using Abp.Runtime.Security;
    using Abp.Web.Models;
    using AutoMapper.Configuration;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.IdentityModel.Tokens;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using FFF.Test.DataAccess.Framework;
    using Ydm.Extensions;

    /// <summary>
    /// 枚举服务
    /// </summary>
    [YdmApiVersionAttribute(Version = "My")]
    public class EnumAppService : YdmAbpAppServiceBase, IEnumAppService
    {
        private readonly IDbContextProvider<MyDbContext> _dbContextProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="EnumAppService"/> class.
        /// toke构造方法初始化配置对象
        /// </summary>
        /// <param name="dbContextProvider">dbContextProvider</param>
        public EnumAppService(IDbContextProvider<MyDbContext> dbContextProvider)
        {
            this._dbContextProvider = dbContextProvider;
        }

        /// <summary>
        /// 获取Gird的数据
        /// </summary>
        /// <param name="inDto">表格接收参数</param>
        /// <returns>grid</returns>
        [HttpPost]
        [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
        public async Task<List<EnumOutDto>> GetEnumArray(EnumInDto inDto)
        {
            string enumType = $"FFF.Test.Entity.{inDto.Module}.{inDto.EnumTypeName}";
            Assembly autoAssembly = Assembly.Load("FFF.Test.Entity");
            Type type = autoAssembly.GetType(enumType);

            List<EnumOutDto> enumOutDtos = new List<EnumOutDto>();

            // 获取枚举的描述
            foreach (var value in Enum.GetValues(type))
            {
                EnumOutDto outDto = new EnumOutDto();
                var desc = value.ToString();
                if (desc == "None")
                {
                    if (inDto.IsShowAll == 1)
                    {
                        desc = "全部";
                    }
                    else
                    {
                        desc = string.Empty;
                    }
                }

                outDto.Text = desc;
                outDto.Value = (int)value;
                enumOutDtos.Add(outDto);
            }

            return await Task.FromResult(enumOutDtos);
        }
    }
}